#include <asm/regdef.h>
//#include "../include/asm/cp0regdef.h"
#include <asm/asm.h>
#include <trap.h>
.data
.global	KERNEL_SP;
KERNEL_SP:
.word		0



.text
//.set noreorder
//	.set noat
LEAF(env_pop_tf)
	// Shutdown interrupts
	csrr	t0, sstatus                   
	ori	t0, t0, 0x3                          
	xori	t0, t0, 0x3                          
	csrrw	t0, sstatus, t0

	move	sp, a0

//	ld	t1, TF_EPC(sp)
	ld	t1, TF_PC(sp)
	ld	t0, TF_IE(sp)
	csrrw	x0, sepc, t1
	csrrw	x0, sie, t0

	ld	x31, TF_REG31(sp)
	ld	x30, TF_REG30(sp)
	ld	x29, TF_REG30(sp)
	ld	x28, TF_REG28(sp)
	ld	x27, TF_REG30(sp)
	ld	x26, TF_REG30(sp)
	ld	x25, TF_REG25(sp)
	ld	x24, TF_REG24(sp)
	ld	x23, TF_REG23(sp)
	ld	x22, TF_REG22(sp)
	ld	x21, TF_REG21(sp)
	ld	x20, TF_REG20(sp)
	ld	x19, TF_REG19(sp)
	ld	x18, TF_REG18(sp)
	ld	x17, TF_REG17(sp)
	ld	x16, TF_REG16(sp)
	ld	x15, TF_REG15(sp)
	ld	x14, TF_REG14(sp)
	ld	x13, TF_REG13(sp)
	ld	x12, TF_REG12(sp)
	ld	x11, TF_REG11(sp)
	ld	x10, TF_REG10(sp)
	ld	x9, TF_REG9(sp)
	ld	x8, TF_REG8(sp)
	ld	x7, TF_REG7(sp)
	ld	x6, TF_REG6(sp)
	ld	x5, TF_REG5(sp)
	ld	x4, TF_REG4(sp)
	ld	x3, TF_REG3(sp)

	ld	x1, TF_STATUS(sp)    			 
	csrrw	x1, sstatus, x1
	ld	x1, TF_REG1(sp)
	ld	x2, TF_REG2(sp) // Restore sp
	sret // Return to addr in 'sepc'
/*
    .set	mips1          
    //1:	j	1b
    nop    
    move		k0,a0 
    mtc0	a1,CP0_ENTRYHI


    mfc0	t0,CP0_STATUS                    
    ori	t0,0x3                          
    xori	t0,0x3                          
    mtc0	t0,CP0_STATUS                    

lw	v1,TF_LO(k0)                                       
    mtlo	v1                               
    lw	v0,TF_HI(k0)                     
lw	v1,TF_EPC(k0)                    
    mthi	v0                               
    mtc0	v1,CP0_EPC                       
    lw	$31,TF_REG31(k0)                 
    lw	$30,TF_REG30(k0)     
    lw	$29,TF_REG29(k0)            
    lw	$28,TF_REG28(k0)                 
    lw	$25,TF_REG25(k0)                 
    lw	$24,TF_REG24(k0)                 
    lw	$23,TF_REG23(k0)                 
    lw	$22,TF_REG22(k0)                 
    lw	$21,TF_REG21(k0)                 
    lw	$20,TF_REG20(k0)                 
    lw	$19,TF_REG19(k0)                 
    lw	$18,TF_REG18(k0)                 
    lw	$17,TF_REG17(k0)                 
    lw	$16,TF_REG16(k0)                 
    lw	$15,TF_REG15(k0)                 
    lw	$14,TF_REG14(k0)                 
    lw	$13,TF_REG13(k0)                 
    lw	$12,TF_REG12(k0)                 
    lw	$11,TF_REG11(k0)                 
    lw	$10,TF_REG10(k0)                 
    lw	$9,TF_REG9(k0)                   
    lw	$8,TF_REG8(k0)                   
    lw	$7,TF_REG7(k0)                   
    lw	$6,TF_REG6(k0)                   
    lw	$5,TF_REG5(k0)                   
    lw	$4,TF_REG4(k0)                   
    lw	$3,TF_REG3(k0)                   
    lw	$2,TF_REG2(k0)                   
    lw  $1,TF_REG1(k0)

    lw  k1,TF_PC(k0)

    lw	k0,TF_STATUS(k0)                 
    nop					 
    mtc0	k0,CP0_STATUS



    j	k1
    rfe
    nop

*/
END(env_pop_tf)

LEAF(lcontext)
	.extern	mCONTEXT
	la	t0, mCONTEXT
	sd	a0, 0(t0)
	csrrw	a0, satp, a0
	sfence.vma
	jr ra
/*
    .extern	mCONTEXT
    sw		a0,mCONTEXT
    jr	ra
    nop
*/
END(lcontext)


